<?php
class DBConnection {
	private $conn;
	private $driver;

	public function __construct() {
		$dbinfo = Configure::read('dbinfo');
		$this->driver = $dbinfo['dbdriver'];
		
		if ($this->driver == "mysql") {
			$this->conn = mysql_connect($dbinfo['dbhost'], $dbinfo['dbuser'], $dbinfo['dbpass']);
			
			if (!$this->conn) {
				throw new CherryException("DB Error: Cannot connect to database", 500);
			}
			
			if (!mysql_select_db($dbinfo['dbname'], $this->conn)) {
				throw new CherryException("DB Error: ". mysql_error(), 500);
			}
		
		} else if ($this->driver == "mysqli") {
			$this->conn = mysqli_connect($dbinfo['dbhost'], $dbinfo['dbuser'], $dbinfo['dbpass']);
			
			if (!$this->conn) {
				throw new CherryException("DB Error: Cannot connect to database", 500);
			}
			
			if (!mysqli_select_db($dbinfo['dbname'], $this->conn)) {
				throw new CherryException("DB Error: ". mysqli_error(), 500);
			}
			
		}  else if ($this->driver == "pgsql") {
			if (trim($dbinfo['dbport']) != "") {
				$port = "port=".$dbinfo['dbport'];
			} 
			$this->conn = pg_connect("host=".$dbinfo['dbhost']." ". $port ." dbname=".$dbinfo['dbname']." user=".$dbinfo['dbuser']." password=".$dbinfo['dbpass']);
		
		} else {
			throw new CherryException($dbinfo['dbdriver']." database driver doesn't exist", 500);
		}
		
		if (!$this->conn) {
			throw new CherryException("DB Error: Cannot connect to database", 500);
		}
		if ($this->driver == "oci8") {
			$this->conn = oci_connect($dbinfo['dbhost'], $dbinfo['dbuser'], $dbinfo['dbpass']);
			
			if(!$this->conn) {
				thrwo new CherryException("DB Error: Cannot connect to database", 500);
			}
		} else {
			throw new CherryException($dbinfo['dbdriver']." database driver doesn't exist", 500);
		}
		
		if ($this->driver == "mssql") {
			$this->conn = mssql_connect($dbinfo['dbhost'], $dbinfo['dbuser'], $dbinfo['dbpass']);
			
			if(!$this->conn){
				throw new CherryException("DB Error: Cannot connect to database", 500);
			}
		} else {
			throw new CherryException($dbinfo['dbdriver']." database driver doesn't exist", 500); 
		}
		
	}
	
	public function query($sql) {
		if ($this->driver == "pgsql") {
			$res = pg_query ($this->conn, $sql);
			
			if (!$res) {
				throw new CherryException("DB Error: ".pg_last_error(), 400);
			}
			return pg_fetch_all($res);
			
		} else if ($this->driver == "mysql") {
			$res = mysql_query ($this->conn, $sql);
			
			if (!$res) {
				throw new CherryException("DB Error: ".mysql_error(), 400);
			}
			$data = array();
			
			while ($row = mysql_fetch_assoc($res)) {
				$data[] = $row;
			}
			return $data;
			
		} else if ($this->driver == "oci8") {
			$res = oci_parse($this->conn, $sql);
			
			if (!$res) {
				throw nee CherryException("DB Error: ".oci_error(), 400);
			}
			$data = array();
			
			while ($row = oci_fetch_array($res)) {
				$data[] = $row;
			}
			return $data;
			
		} else if ($this->driver == "mssql") {
			$res = mssql_query($sql, $this->conn);
			
			if (!$res) {
				thorw new CherryException("DB Error: ".mssql_min_error_severity(1), 400);
			}
			$data = array();
			
			while ($row = mssql_fetch_array($res)){
				$data[] = $row;
			}
			return $data;
			
		} else if ($this->driver == "mysqli") {
			$res = mysqli_query ($this->conn, $sql);
			
			if (!$res) {
				throw new CherryException("DB Error: ".mysqli_error(), 400);
			}
			
			//ver esto
			return array();
		}
	}

	public function execute($sql) {
		if ($this->driver == "pgsql") {
			$exec = pg_execute ($this->conn, $sql);
			
			if (!$exec) {
				throw new CherryException("DB Error: ".pg_last_error(), 400);
			}
			return $exec;
		}
	}
}
?>